const app = getApp()
const Api = require("../../../utils/api.js")
const ApiRequest = require("../../../utils/request.js");
 
Page({

  /**
   * 页面的初始数据
   */
  data: {
    visitTotal: "男",
    starCount: "门诊科",
    forksCount: "一级",
    modalName: "",
    loginUser: null,
    qrUrl: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let doctorInfo = options.doctorInfo
    this.setData({
      doctorInfo: doctorInfo
    })
    let loginUser = app.globalData.loginUser;
    this.setData({
      loginUser: loginUser
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  getFlowList: function(e) {
    var self = this
    let state = e.currentTarget.dataset.state
    let doctorID = app.globalData.loginUser.id
    var url = Api.find_flow_doctor
    var params = {
      doctorId: doctorID,
      state: state
    };
    var method = 'GET';
    var header = "application/json";
    var success = function(res) {
      let data = res.data
      if (res.code == 200 && data.length != 0) {
        wx.navigateTo({
          url: '../list/list?flowList=' + JSON.stringify(data) + '&state=' + state,
        })
      } else if (res.code == 200 && data.length == 0) {
        wx.showToast({
          icon: 'none',
          title: '您目前没有在治患者',
        })
      }
    }
    ApiRequest.request(url, params, method, header, success, self.fail);
  },
  fail: function(e) {
    console.log("请求失败：", e)
  },
  /**
   * 获取二维码
   */
  showModal(e) {
    let self = this
    wx.showLoading({
      title: '获取中',
    })
    let loginUser = app.globalData.loginUser
    var url = Api.qrCode + '/' + loginUser.id;
    var params = {
      doctorID: loginUser.id,
    };
    var method = 'POST';
    var header = "application/x-www-form-urlencoded";
    var success = function(res) {
      let data = res.data
      let qrUrl = app.globalData.baseURL + data
      // console.log("qrUrl", qrUrl)
      self.setData({
        qrUrl: qrUrl,
        modalName: e.currentTarget.dataset.target
      })
      // 确保qrURL已赋值
      var qrURLConfirm = setInterval(function() {
        if(self.data.qrUrl!=''){
          wx.hideLoading()
        }
      }, 500)
      setTimeout(function() {
        clearInterval(qrURLConfirm)
      }, 2000)
    }
    var fail = function(data) {

    }
    ApiRequest.request(url, params, method, header, success, fail);
  },
  hideModal(e) {
    this.setData({
      modalName: ""
    })
  },
})